import path from 'path'

import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import viteCompression from 'vite-plugin-compression'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

const pathSrc = path.resolve(__dirname, 'src')
const pathRoot = path.resolve(__dirname, './')

export default defineConfig({
  build: {
    sourcemap: false,
    rollupOptions: {
      output: {
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
      },

      manualChunks(id) {
        if (id.includes('node_modules')) {
          return 'vendor'
        }
      }
    }
  },
  plugins: [
    Vue(),
    AutoImport({
      imports: ['vue', 'vue-router'],

      resolvers: [
        ElementPlusResolver(),

        IconsResolver({
          prefix: 'i'
        })
      ],

      dts: path.resolve(pathRoot, 'auto-imports.d.ts')
    }),

    Components({
      resolvers: [ElementPlusResolver()],

      dts: path.resolve(pathRoot, 'components.d.ts')
    }),

    Icons({
      compiler: 'vue3',
      autoInstall: true
    }),

    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      symbolId: 'icon-[dir]-[name]'
    }),

    viteCompression({
      verbose: true,
      threshold: 10240,
      disable: false,
      deleteOriginFile: false,
      algorithm: 'gzip',
      ext: '.gz'
    })
  ],
  server: {
    hmr: true,
    open: false,
    host: '0.0.0.0',
    https: false,
    proxy: {
      '/api': {
        target: 'http://192.168.0.52:8188/',
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  resolve: {
    alias: {
      '@': pathSrc
    }
  }
})
